package controllers

import (
	// m "github.com/beego/admin/src/models"
	// "github.com/astaxie/beego/orm"
	"crypto/md5"
	"encoding/hex"
	m "github.com/3xxx/engineercms/models"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"

	// "github.com/astaxie/beego/logs"
	"github.com/tealeg/xlsx"
	"os"
	"strconv"
	"strings"
	"time"
)

type UserController struct {
	beego.Controller
}

func (c *UserController) Index() {
	username, role, uid, isadmin, islogin := checkProdRole(c.Ctx)
	c.Data["Username"] = username
	c.Data["Ip"] = c.Ctx.Input.IP()
	c.Data["role"] = role
	c.Data["IsAdmin"] = isadmin
	c.Data["IsLogin"] = islogin
	c.Data["Uid"] = uid
	if !isadmin { //&& uname != category.Author
		route := c.Ctx.Request.URL.String()
		c.Data["Url"] = route
		c.Redirect("/roleerr?url="+route, 302)
		return
	}

	users, count := m.Getuserlist(1, 2000, "Id")
	if c.IsAjax() {
		c.Data["json"] = &map[string]interface{}{"total": count, "rows": &users}
		c.ServeJSON()
		return
	} else {
		c.Data["Users"] = &users
		c.TplName = "user.tpl"
	}
}

//如果不带id则取到所有用户
//如果带id，则取一个用户
func (c *UserController) User() {
	id := c.Ctx.Input.Param(":id")
	c.Data["Id"] = id
	c.Data["Ip"] = c.Ctx.Input.IP()
	// var categories []*models.AdminCategory
	if id == "" { //如果id为空，则查询类别
		users, err := m.GetUsers()
		if err != nil {
			logs.Error(err)
		}
		//如果设置了role,用于onlyoffice的权限设置
		role := c.Input().Get("role")
		if role != "" {
			for _, v := range users {
				v.Role = role
			}
		}
		c.Data["json"] = &users
		c.ServeJSON()
	} else {
		//pid转成64为
		idNum, err := strconv.ParseInt(id, 10, 64)
		if err != nil {
			logs.Error(err)
		}
		user := m.GetUserByUserId(idNum)
		if err != nil {
			logs.Error(err)
		}

		// var users1 []*m.User
		users := make([]*m.User, 1)
		users[0] = &user
		// users = append(users, &user...)
		c.Data["json"] = users //取到一个用户数据，不是数组，所以table无法显示
		c.ServeJSON()
	}
}

//用户登录查看自己的资料_不是这个，是GetUserByUsername
func (c *UserController) View() {
	username, role, uid, isadmin, islogin := checkProdRole(c.Ctx)
	c.Data["Username"] = username
	c.Data["Ip"] = c.Ctx.Input.IP()
	c.Data["role"] = role
	c.Data["IsAdmin"] = isadmin
	c.Data["IsLogin"] = islogin
	c.Data["Uid"] = uid
	userid, _ := strconv.ParseInt(c.Input().Get("useid"), 10, 64)
	user := m.GetUserByUserId(userid)
	c.Data["User"] = user
	c.TplName = "admin_user_view.tpl"
}

//添加用户
func (c *UserController) AddUser() {
	var user m.User
	user.Username = c.Input().Get("username")
	user.Nickname = c.Input().Get("nickname")

	Pwd1 := c.Input().Get("password")
	md5Ctx := md5.New()
	md5Ctx.Write([]byte(Pwd1))
	cipherStr := md5Ctx.Sum(nil)
	user.Password = hex.EncodeToString(cipherStr)

	user.Email = c.Input().Get("email")
	user.Department = c.Input().Get("department")
	user.Secoffice = c.Input().Get("secoffice")
	user.Ip = c.Input().Get("ip")
	user.Port = c.Input().Get("port")
	statusint, err := strconv.Atoi(c.Input().Get("status"))
	if err != nil {
		logs.Error(err)
	}
	user.Status = statusint
	user.Role = c.Input().Get("role")
	id, err := m.SaveUser(user)
	if err == nil && id > 0 {
		// c.Rsp(true, "Success")
		// return
		c.Data["json"] = "ok"
		c.ServeJSON()
	} else {
		// c.Rsp(false, err.Error())
		logs.Error(err)
		// return
	}
}

// @Title post wx userpassword by uid
// @Description post user password by uid
// @Param uid query string  true "The id of user"
// @Param oldpass query string  true "The oldPassword of user"
// @Param newpass query string  true "The newpassword of user"
// @Param id path string  true "The id of wx"
// @Success 200 {object} models.AddArticle
// @Failure 400 Invalid page supplied
// @Failure 404 articl not found
// @router /updatewxuser [post]
//在线修改保存某个字段
func (c *UserController) UpdateUser() {
	oldpass := c.Input().Get("oldpass")
	uid := c.Input().Get("uid")
	id, err := strconv.ParseInt(uid, 10, 64)
	if err != nil {
		logs.Error(err)
	}
	//取出用户旧密码
	user := m.GetUserByUserId(id)
	md5Ctx := md5.New()
	md5Ctx.Write([]byte(oldpass))
	cipherStr := md5Ctx.Sum(nil)
	Password := hex.EncodeToString(cipherStr)
	if user.Password == Password {
		newpass := c.Input().Get("newpass")
		err = m.UpdateUser(id, "Password", newpass)
		if err != nil {
			logs.Error(err)
			c.Data["json"] = "wrong"
			c.ServeJSON()
		} else {
			c.Data["json"] = "ok"
			c.ServeJSON()
		}
	} else {
		c.Data["json"] = "旧密码不正确"
		c.ServeJSON()
	}
}

//删除用户
func (c *UserController) DeleteUser() {
	ids := c.GetString("ids")
	array := strings.Split(ids, ",")
	for _, v := range array {
		//id转成64位
		idNum, err := strconv.ParseInt(v, 10, 64)
		if err != nil {
			logs.Error(err)
		}
		status, err := m.DelUserById(idNum)
		if err == nil && status > 0 {
			c.Data["json"] = "ok"
			c.ServeJSON()
		} else if err != nil {
			logs.Error(err)
		}
	}
}

//用户查看自己，修改密码等
func (c *UserController) GetUserByUsername() {
	username, role, uid, isadmin, islogin := checkProdRole(c.Ctx)
	c.Data["Username"] = username
	c.Data["Ip"] = c.Ctx.Input.IP()
	c.Data["role"] = role
	c.Data["IsAdmin"] = isadmin
	c.Data["IsLogin"] = islogin
	c.Data["Uid"] = uid
	if islogin != true {
		route := c.Ctx.Request.URL.String()
		c.Data["Url"] = route
		c.Redirect("/roleerr?url="+route, 302)
		return
	}
	c.TplName = "user_view.tpl"
}

//用户个人数据，填充table，以便编辑
func (c *UserController) Usermyself() {
	username, role, uid, isadmin, islogin := checkProdRole(c.Ctx)
	c.Data["Username"] = username
	c.Data["Ip"] = c.Ctx.Input.IP()
	c.Data["role"] = role
	c.Data["IsAdmin"] = isadmin
	c.Data["IsLogin"] = islogin
	c.Data["Uid"] = uid
	user, err := m.GetUserByUsername(username)
	if err != nil {
		logs.Error(err)
	}
	users := make([]*m.User, 1)
	users[0] = &user
	c.Data["json"] = users
	c.ServeJSON()
}

//上传excel文件，导入到数据库
//引用来自category的查看成果类型里的成果
func (c *UserController) ImportUsers() {
	//获取上传的文件
	_, h, err := c.GetFile("usersexcel")
	if err != nil {
		logs.Error(err)
	}
	// beego.Info(h.path)
	// var attachment string
	var path string

	// var filesize int64
	if h != nil {
		//保存附件
		path = "./attachment/" + h.Filename    // 关闭上传的文件，不然的话会出现临时文件不能清除的情况
		err = c.SaveToFile("usersexcel", path) //.Join("attachment", attachment)) //存文件    WaterMark(path)    //给文件加水印
		if err != nil {
			logs.Error(err)
			c.Data["json"] = "err保存文件失败"
			c.ServeJSON()
		} else {
			var user m.User
			//读出excel内容写入数据库
			xlFile, err := xlsx.OpenFile(path) //
			if err != nil {
				logs.Error(err)
			}
			for _, sheet := range xlFile.Sheets {
				for i, row := range sheet.Rows {
					if i != 0 { //忽略第一行标题
						// 这里要判断单元格列数，如果超过单元格使用范围的列数，则出错for j := 2; j < 7; j += 5 {
						j := 1
						if len(row.Cells) >= 2 { //总列数，从1开始
							user.Username = row.Cells[j].String()
							if err != nil {
								logs.Error(err)
							}
						}
						if len(row.Cells) >= 3 {
							user.Nickname = row.Cells[j+1].String()
							if err != nil {
								logs.Error(err)
							}
						}
						if len(row.Cells) >= 4 {
							Pwd1 := row.Cells[j+2].String()
							if err != nil {
								logs.Error(err)
							}

							md5Ctx := md5.New()
							md5Ctx.Write([]byte(Pwd1))
							cipherStr := md5Ctx.Sum(nil)
							user.Password = hex.EncodeToString(cipherStr)
						}
						if len(row.Cells) >= 5 {
							user.Email = row.Cells[j+3].String()
							if err != nil {
								logs.Error(err)
							}
						}
						if len(row.Cells) >= 6 {
							user.Department = row.Cells[j+4].String()
							if err != nil {
								logs.Error(err)
							}
						}
						if len(row.Cells) >= 7 {
							user.Secoffice = row.Cells[j+5].String()
							if err != nil {
								logs.Error(err)
							}
						}
						if len(row.Cells) >= 8 {
							user.Ip = row.Cells[j+6].String()
							if err != nil {
								logs.Error(err)
							}
						}
						if len(row.Cells) >= 9 {
							user.Port = row.Cells[j+7].String()
							if err != nil {
								logs.Error(err)
							}
						}
						if len(row.Cells) >= 10 {
							status := row.Cells[j+8].String()
							if err != nil {
								logs.Error(err)
							}
							status1, err := strconv.Atoi(status)
							if err != nil {
								logs.Error(err)
							}
							user.Status = status1
						}
						if len(row.Cells) >= 11 {
							role := row.Cells[j+9].String()
							if err != nil {
								logs.Error(err)
							}

							user.Role = role
							user.LastLoginTime = time.Now()
							_, err = m.SaveUser(user) //如果姓名重复，也要返回uid
							if err != nil {
								logs.Error(err)
							}
						}
					}
				}
			}
			//删除附件
			err = os.Remove(path)
			if err != nil {
				logs.Error(err)
			}
			c.Data["json"] = "ok"
			c.ServeJSON()
		}
	} else {
		c.Data["json"] = "err文件为空！"
		c.ServeJSON()
	}
}

func (this *UserController) Roleerr() {
	// url := this.Input().Get("url")
	url1 := this.Input().Get("url") //这里不支持这样的url，http://192.168.9.13/login?url=/topic/add?id=955&mid=3
	url2 := this.Input().Get("level")
	url3 := this.Input().Get("key")
	var url string
	if url2 == "" {
		url = url1
	} else {
		url = url1 + "&level=" + url2 + "&key=" + url3
	}
	this.Data["Url"] = url
	this.TplName = "role_err.tpl"
}
